package com.wcl.nullmusic.ui.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wcl.nullmusic.R;
import com.wcl.nullmusic.Mp3Info;
import com.wcl.nullmusic.ui.interfaces.MyMusicOnClickListener;
import com.wcl.nullmusic.utils.LocalMp3InfoUtils;
import com.wcl.nullmusic.utils.BaseUtils;

import java.io.File;
import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class MyMusicRecyclerViewAdapter extends RecyclerView.Adapter<MyMusicRecyclerViewAdapter.ViewHolder> {
    private List<Mp3Info> datas;
    private MyMusicOnClickListener listener;
    private View view;
    private Context context;
    private AnimationDrawable animationDrawable;

    public MyMusicRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void setOnMyMusicClickListener(MyMusicOnClickListener listener) {
        this.listener = listener;
    }

    public void addData(List<Mp3Info> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mymusic_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            Mp3Info info = datas.get(position);
            if (!TextUtils.isEmpty(info.getPic()))
                Picasso.with(context).load(new File(info.getPic())).placeholder(R.mipmap.ic_live_voice_press)
                        .error(R.mipmap.ic_live_voice_press).into(holder.mymusic_item_header);
            else
                Picasso.with(context).load(info.getPic()).placeholder(R.mipmap.ic_live_voice_press)
                        .error(R.mipmap.ic_live_voice_press).into(holder.mymusic_item_header);
            //      holder.mymusic_item_header.setImageResource(R.mipmap.ic_live_voice_press);
            holder.mymusic_item_duration.setText(BaseUtils.toDurtion(info.getDuration()));
            holder.mymusic_item_author.setText(info.getArtist());
            holder.mymusic_item_songName.setText(info.getTitle());

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.setMyMusicOnClickListener(holder.getLayoutPosition(),holder.mymusic_anim);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mymusic_item_header, mymusic_anim;
        private TextView mymusic_item_duration, mymusic_item_songName, mymusic_item_author;

        public ViewHolder(View itemView) {
            super(itemView);
            mymusic_item_header = (ImageView) itemView.findViewById(R.id.mymusic_item_header);
            mymusic_anim = (ImageView) itemView.findViewById(R.id.mymusic_anim);
            mymusic_item_duration = (TextView) itemView.findViewById(R.id.mymusic_item_duration);
            mymusic_item_songName = (TextView) itemView.findViewById(R.id.mymusic_item_songName);
            mymusic_item_author = (TextView) itemView.findViewById(R.id.mymusic_item_author);
        }
    }
}
